######################################
# target
######################################
TARGET = geek_lite_board

######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -O3

#######################################
# paths
#######################################
# Build path
BUILD_DIR = build

######################################
# source
######################################
# C sources
C_SOURCES	+= 	core/src/stm32f4xx_it.c \
				core/src/system_stm32f4xx.c \
				driver/src/misc.c \
				driver/src/stm32f4xx_adc.c \
				driver/src/stm32f4xx_can.c \
				driver/src/stm32f4xx_cec.c \
				driver/src/stm32f4xx_crc.c \
				driver/src/stm32f4xx_cryp.c \
				driver/src/stm32f4xx_cryp_aes.c \
				driver/src/stm32f4xx_cryp_des.c \
				driver/src/stm32f4xx_cryp_tdes.c \
				driver/src/stm32f4xx_dac.c \
				driver/src/stm32f4xx_dbgmcu.c \
				driver/src/stm32f4xx_dcmi.c \
				driver/src/stm32f4xx_dfsdm.c \
				driver/src/stm32f4xx_dma.c \
				driver/src/stm32f4xx_dma2d.c \
				driver/src/stm32f4xx_dsi.c \
				driver/src/stm32f4xx_exti.c \
				driver/src/stm32f4xx_flash.c \
				driver/src/stm32f4xx_flash_ramfunc.c \
				driver/src/stm32f4xx_fmc.c \
				driver/src/stm32f4xx_fmpi2c.c \
				driver/src/stm32f4xx_gpio.c \
				driver/src/stm32f4xx_hash.c \
				driver/src/stm32f4xx_hash_md5.c \
				driver/src/stm32f4xx_hash_sha1.c \
				driver/src/stm32f4xx_i2c.c \
				driver/src/stm32f4xx_iwdg.c \
				driver/src/stm32f4xx_lptim.c \
				driver/src/stm32f4xx_ltdc.c \
				driver/src/stm32f4xx_pwr.c \
				driver/src/stm32f4xx_qspi.c \
				driver/src/stm32f4xx_rcc.c \
				driver/src/stm32f4xx_rng.c \
				driver/src/stm32f4xx_rtc.c \
				driver/src/stm32f4xx_sai.c \
				driver/src/stm32f4xx_sdio.c \
				driver/src/stm32f4xx_spdifrx.c \
				driver/src/stm32f4xx_spi.c \
				driver/src/stm32f4xx_syscfg.c \
				driver/src/stm32f4xx_tim.c \
				driver/src/stm32f4xx_usart.c \
				driver/src/stm32f4xx_wwdg.c \
				usb/USB_Driver/src/usb_bsp.c \
				usb/USB_Driver/src/usb_core.c \
				usb/USB_Driver/src/usb_dcd.c \
				usb/USB_Driver/src/usb_dcd_int.c \
				usb/USB_Library/Core/src/usbd_cdc_vcp.c \
				usb/USB_Library/Core/src/usbd_core.c \
				usb/USB_Library/Core/src/usbd_desc.c \
				usb/USB_Library/Core/src/usbd_ioreq.c \
				usb/USB_Library/Core/src/usbd_req.c \
				usb/USB_Library/Core/src/usbd_usr.c \
				usb/USB_Library/Class/cdc/src/usbd_cdc_core.c \

C_SOURCES	+=	$(wildcard utils/src/*.c) \
				$(wildcard app/src/*.c) \
				$(wildcard robot/src/*.c) \
				$(wildcard modular/src/*.c) \
				$(wildcard main/src/*.c)

# ASM sources
ASM_SOURCES = startup_stm32f427_437xx.s

#######################################
# binaries
#######################################
PREFIX =arm-none-eabi-
#arm-none-linux-gnueabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4

# fpu
FPU = -mfpu=fpv4-sp-d16

# float-abi
FLOAT-ABI = -mfloat-abi=hard

# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = 

# C defines
C_DEFS = -DUSE_STDPERIPH_DRIVER \
-DSTM32F427_437xx \
-DUSE_STM324x9I_EVAL \
-DUSE_USB_OTG_FS \
-DUSE_ULPI_PHY

# AS includes
AS_INCLUDES = 

# C includes
C_INCLUDES	+=	-Iapp/inc \
				-Imain/inc \
				-Irobot/inc \
				-Imodular/inc \
				-Iutils/inc \
				-Icore/inc \
				-Iusb/USB_Driver/inc \
				-Iusb/USB_Library/Class/cdc/inc \
				-Iusb/USB_Library/Core/inc \
				-Idriver/inc

include liteos_m.mk

# compile gcc flags
#ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -fdata-sections -ffunction-sections

#CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

#######################################
# LDFLAGS
######################################
# link script
LDSCRIPT = STM32F437IIHx_FLASH.ld

# libraries
LIBS = -lc -lm -lnosys
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -std=gnu11 -D_MICROLIB

#后续待解决浮点数据的输出问题
LDFLAGS += -u _printf_float

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# build the application
#######################################
# list of objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir $@		

#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)
  
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***
